<?php
class PostsController extends AppController{
	public $components = array('Session');
	function index($title=null){
		$this->layout = 'simple_cms';
		$this->loadModel('Contact');
		
		$artical=array();
		
		$base_url = 'http://'.$_SERVER['HTTP_HOST'].$this->base ;
		$this->set("base_url",$base_url);
		
		
		
		//echo $this->Auth->user('id');
		if ($this->Auth->user('id')!=" "){
			$log_link = "<a href='".$base_url."/Pages/'>Admin</a> &nbsp; <a style='color:#fff;' href='".$base_url."/users/logout'>Logout</a>";
		}
		if($this->Auth->user('id') == ''){
			$log_link = "<a href='".$base_url."/Pages/'>Admin</a> &nbsp; <a style='color:#fff;' href='".$base_url."/users/login'>Login</a>";
		}

		$this->set("log_link",$log_link);
		
		//query for header
		$header = $this->Post->query("select * from headers");
		$logo = $header[0]['headers']['logo'];
		if(!empty($logo)){
			$this->set('logo','<div class="span1 header_left"><img src="'.$base_url.'/app/webroot/upload/'.$logo.'" width="100"></div>');
			$this->set('site_name','<div class="span9 site_name">'.$header[0]['headers']['site_name'].'</div>');
		}else{
			$this->set('logo','');
			$this->set('site_name','<div class="span10 site_name">'.$header[0]['headers']['site_name'].'</div>');
		}
		$this->set('header_image',$header[0]['headers']['header_image']);
		
		
		
		// query for menu
		$menu=$this->Post->query("select * from menus where menu_type='top' order by menu_order asc");
		
		// set menu using function set_menu
		$set_menu=$this->Post->set_menu($menu,$base_url);
		
		// set variable for view to display menu
		$this->set('menus',$set_menu);
		
		// Display Slide show
		$slide_show=$this->Post->display_slide_show($title,$base_url);
		$this->set('slide',$slide_show);
		
		
		
		//condition for content
		if($title!=""){
			$page=$this->Post->get_page($title);
			$count_page = count($page);
			$page_id = $page[0]['pages']['id'];
			$page_title = $page[0]['pages']['title'];
			
			if($this->Post->is_contact($page_id)){
				$content = $page[0]['pages']['page'];
				$contact_form = $this->Post->contact_form($this->base,$title);
				$this->set('contact_form',$contact_form);
			}else{
				$content = $page[0]['pages']['page'];
			}
			
			$this->set('title_for_layout', $title);
			$this->set('count_page', $count_page);
			$this->set('page_title',$page_title);
			$this->set('content',$content);
			
		}else{
			$page=$this->Post->set_default();
			$count_page = count($page);
			

			$title = $page[0]['pages']['title'];
			$content = $page[0]['pages']['page'];
			
			
			$this->set('title_for_layout',$title);
			$this->set('count_page', $count_page);
			$this->set('page_title',$title);
			$this->set('content',$content);
		}
		
		
		// query for footer menu
		$footer_menu = $this->Post->query("select * from menus where menu_type='footer'");
		
		// set variable for view to display footer menu
		$this->set('footer_menus',$footer_menu);
	}
	
	function mail(){
		$data =$_REQUEST['data'];
		$title = $data['Contact']['title'];
		$mail = $this->Post->send_mail($data);
		
		if($mail){
			$this->redirect(array('action' => 'index/'.$title));
		}else{
			$this->redirect(array('action' => 'index/'.$title));
		}
	}
	

}
?>